package com.gopay.solution.domain.bo.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.stereotype.Service;

import com.gopay.common.constants.rcs.TranDirection;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.solution.TranLimitReq;
import com.gopay.common.domain.solution.TranLimitRes;
import com.gopay.solution.domain.po.SolutionRuleDtl;
import com.gopay.solution.domain.po.SsTransLimitConfDtl;
import com.gopay.solution.domain.vo.Event;
import com.gopay.solution.domain.vo.Result;

/**
 * 
 * @SsCapSoluRuleExecutor.java
 * @author liujy
 * @2013-5-7 上午9:24:13 www.gopay.com.cn Inc.All rights reserved.
 */
@Service(value = "ssTransLimitConfSoluRuleExecutor")
public class SsTransLimitConfSoluRuleExecutor extends AbstractSolutionRuleExecutor {


    @Override
    public Result execute(List<SolutionRuleDtl> rules, Event event) {
        

        Result res = new Result();
        res.setSolved(false);
        res.setFailedReason("");
        res.setDetail(null);

        if (null == rules || rules.isEmpty()) {
            res.setFailedReason("查询不到限额配置 方案记录");
            return res;
        }

        Serializable input = event.getDetail();

        TranLimitReq req = (TranLimitReq) input;
        TranDirection td = req.getTranDirection();
        IntTxnCd intTxnCd = req.getIntTxnCd();
        String acctType = req.getAcctType();

        for (SolutionRuleDtl rule : rules) {
            
            SsTransLimitConfDtl dtl = (SsTransLimitConfDtl)rule;
            if (null != dtl) {
                if(td.value.equals(dtl.getTranDirection())
                        && intTxnCd.value.equals(dtl.getGopayIntTxnCd())
                        && acctType.equals(dtl.getAcctType())){
                    TranLimitRes res2 = new TranLimitRes();
                    res2.setDayLimit(dtl.getDayLimit());
                    res2.setDayTimes(dtl.getDayTimes());
                    res2.setOnceLimit(dtl.getOnceLimit());
                    res2.setSamePayeeLimit(dtl.getSavePayeeLimit());
                    res2.setEnabled(dtl.getIsEnabled() == 1 ? true : false);
                    res.setDetail(res2);
                    break;
                }
            }
        }

        

        if (null != res.getDetail()) {
            res.setSolved(true);
        }
        return res; 
    }
}
